// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <cantor/unresolved/visitor.h>

namespace cantor::unresolved {
    class UnresolvedColumnName;
    class UnresolvedStatement;
    class UnresolvedRowExpr;
    class UnresolvedAssignment;
    class UnresolvedTableName;

    class UnresolvedInsertStmt : public UnresolvedVisitor<UnresolvedInsertStmt> {
    public:
        static constexpr const cantor::pb::ast::StmtType TYPE = cantor::pb::ast::StmtType::STMT_INSERT;

        UnresolvedInsertStmt(const cantor::pb::ast::InsertStmt &node) : instance(node) {
        }

        UnresolvedInsertStmt(const cantor::pb::ast::Statement &node) : instance(node.insert_stmt()) {
        }

        ~UnresolvedInsertStmt() override = default;

        const pb::Location &location_impl() const {
            return instance.location();
        }

        const pb::Status &error_status_impl() const {
            return instance.status();
        }

        bool is_replace(bool value = false) const {
            if (instance.has_is_replace()) {
                return instance.is_replace();
            }
            return value;
        }

        bool is_merge(bool value = false) const {
            if (instance.has_is_merge()) {
                return instance.is_merge();
            }
            return value;
        }

        bool is_ignore(bool value = false) const {
            if (instance.has_is_ignore()) {
                return instance.is_ignore();
            }
            return value;
        }

        bool has_table_name() const {
            return instance.has_table_name();
        }

        UnresolvedTableName table_name() const;

        bool has_subquery_stmt() const {
            return instance.has_subquery_stmt();
        }

        UnresolvedStatement subquery_stmt() const;

        size_t columns_size() const {
            return instance.columns_size();
        }

        UnresolvedColumnName columns(size_t index) const;

        size_t lists_size() const {
            return instance.lists_size();
        }

        UnresolvedRowExpr lists(int index) const;

        size_t partition_names_size() const {
            return instance.partition_names_size();
        }

        std::string_view partition_names(size_t index) const {
            return instance.partition_names(index);
        }

        size_t on_duplicate_size() const {
            return instance.on_duplicate_size();
        }

        UnresolvedAssignment on_duplicate(int index) const;

        const cantor::pb::ast::InsertStmt &instance;
    };
} // namespace cantor::unresolved
